package com.jiawa.wiki.controller;

import com.jiawa.wiki.req.WcategoryQueryReq;
import com.jiawa.wiki.req.WcategorySaveReq;
import com.jiawa.wiki.resp.CommonResp;
import com.jiawa.wiki.resp.WcategoryQueryResp;
import com.jiawa.wiki.resp.PageResp;
import com.jiawa.wiki.service.WcategoryService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/category")
public class WcategoryController {

    @Resource
    private WcategoryService wcategoryService;

    /**
     * 获取所有分类，不含分页
     * @return
     */
    @GetMapping("/all")
    public CommonResp all(){
        CommonResp<List<WcategoryQueryResp>> resp = new CommonResp<>();
        List<WcategoryQueryResp> list = wcategoryService.all();
        resp.setContent(list);
        System.out.println("resp=" + resp);
        return resp;
    }

    /**
     * 获取所有分类含分页
     * @param req
     * @return
     */
    @GetMapping("/list")
    public CommonResp list(@Valid WcategoryQueryReq req){
        CommonResp<PageResp<WcategoryQueryResp>> resp = new CommonResp<>();
        PageResp<WcategoryQueryResp> list = wcategoryService.list(req);
        resp.setContent(list);
        return resp;
    }

    /**
     *保存
     * @param req
     * @return
     * @RequsetBody 接收前端传递的 application/json格式数据
     */
    @PostMapping("/save")
    public CommonResp save(@Valid @RequestBody WcategorySaveReq req){
        CommonResp resp = new CommonResp<>();
        wcategoryService.save(req);
        return resp;
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public CommonResp delete(@PathVariable Long id){
        CommonResp resp = new CommonResp<>();
        wcategoryService.delete(id);
        return resp;
    }
}
